package cn.uway.shiro;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.apache.shiro.cache.CacheManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import cn.uway.cache.RedisCache;

public class JedisShiroCacheManager<K, V> implements CacheManager {

	protected  Logger logger = LoggerFactory.getLogger(this.getClass());
	
	private String keyPrefix = "shiro_redis_cache:";
	
	private final ConcurrentMap<String, ICache<K,V>> caches = new ConcurrentHashMap<String, ICache<K,V>>();
	
	@Autowired
	private RedisCache  redisClient;
	
	public String getKeyPrefix() {
		return keyPrefix;
	}

	public void setKeyPrefix(String keyPrefix) {
		this.keyPrefix = keyPrefix;
	}
	
	@SuppressWarnings("unchecked")
	@Override  
    public  ICache<K, V> getCache(String name) { 		
        logger.debug("获取名称为: {}的RedisCache实例", name);		
        ICache<K,V> cache = caches.get(name);		
		if (cache == null) {
			logger.info("===============getCache  redisClient={}",redisClient);
			// create a new cache instance
			cache = new JedisShiroCache<K, V>(redisClient, keyPrefix);
			
			// add it to the cache collection
			caches.put(name, cache);
		}
		return cache;
    }  
	
	
  
}
